/*
 * MIT License
 *
 * Copyright (c) 2020 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.postman.response.analyzer;

import cn.seaboot.common.bean.Null;
import cn.seaboot.common.core.CommonUtils;
import cn.seaboot.common.reflect.ObjectType;
import cn.seaboot.postman.response.BodyField;
import cn.seaboot.postman.response.Response;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * {@link Response}
 * <p>
 * 生成一些样例数据
 *
 * @author Mr.css
 * @date 2021-01-31 14:29
 */
public class DefaultResponseFactory implements ResponseFactory {

    /**
     * {@inheritDoc}
     */
    @NotNull
    @Override
    public List<Response> createResponse(BodyField bodyField) {
        List<Response> responses = new ArrayList<>(1);
        JSON json = null;
        if (ObjectType.COLLECTION.equals(bodyField.getObjectType())) {
            json = this.getResponseArray(bodyField);
        } else if (ObjectType.OBJECT.equals(bodyField.getObjectType())) {
            json = this.getResponseObject(bodyField);
        }
        if (json != null) {
            Response result = new Response();
            result.setName("Response 样例");
            result.setBody(JSON.toJSONString(json, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue));
            responses.add(result);
        }
        return responses;
    }

    /**
     * 将响应体字段转为JSON对象
     *
     * @param bodyField 响应体字段
     * @return json
     */
    private JSON getResponseObject(BodyField bodyField) {
        Map<String, BodyField> fieldMap = bodyField.getFields();
        if (CommonUtils.isNotEmpty(fieldMap)) {
            JSONObject object = new JSONObject();
            for (Map.Entry<String, BodyField> entry : fieldMap.entrySet()) {
                BodyField field = entry.getValue();
                if (ObjectType.COLLECTION.equals(field.getObjectType())) {
                    object.put(entry.getKey(), this.getResponseArray(field));
                } else if (ObjectType.OBJECT.equals(field.getObjectType())) {
                    object.put(entry.getKey(), this.getResponseObject(field));
                } else {
                    object.put(entry.getKey(), Null.STRING);
                }
            }
            return object;
        }
        return null;
    }

    /**
     * 将响应体字段转为JSONArray对象
     *
     * @param bodyField 响应体字段
     * @return JSONArray
     */
    private JSON getResponseArray(BodyField bodyField) {
        JSONArray array = new JSONArray(1);
        array.add(this.getResponseObject(bodyField));
        return array;
    }
}
